package Operation;
import Book.Book;
import Book.BookList;

import java.util.Arrays;
import java.util.Scanner;

//增加图书
public class AddBook implements IOperation{

    @Override
    public void work(BookList bookList) {
        //添加书籍之前需要检查书架是否为满
        Boolean isFulled = isFull(bookList);
        if(isFulled){
            Book[] books = Arrays.copyOf(bookList.getBooks(), bookList.getDefaultSize()*2);
            bookList.setBooksSpace(bookList.getDefaultSize()*2,books);
        }
        Scanner sc = new Scanner(System.in);
        int count = bookList.getUsedSize();
        System.out.println("添加图书...");
        System.out.println("请输入你要添加书籍的名字：");
        String name = sc.nextLine();
        for(int i = 0; i < count; i++){
            if(bookList.getBooks()[i].getName().equals(name)){
                System.out.println("已经有这本书了，不能重复添加!!!");
                return;
            }
        }
        System.out.println("请输入你要添加书籍的作者：");
        String author = sc.nextLine();

        System.out.println("请输入你要添加书籍的价格：");
        int price = sc.nextInt();

        System.out.println("请输入你要添加书籍的类型：");
        sc.nextLine();
        String type = sc.nextLine();

        bookList.setBooks(count,new Book(name, author, price, type));
        System.out.println("添加完成!");
        bookList.setUsedSize(count + 1);
        System.out.println(bookList.getBooks()[count].toString());
    }
    //判断书架中的书是否满员
    public static boolean isFull(BookList bookList){
        if(bookList.getUsedSize() >= bookList.getDefaultSize()){
            return true;
        }else {
            return false;
        }

    }
}
